import Vue from "vue";
import Vuex from "vuex";
// 获取token|设置token|删除token的函数
import { getToken, setToken, removeToken } from '@/utils/auth'
import { getInfoApi } from "@/api/login";
import persistedstate from 'vuex-persistedstate'

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    token: getToken(),
    userinfo: {
      email: "",
      id: "",
      nickname: "",
      user_pic: "",
      username: "",
    },
  },
  getters: {},
  mutations: {
    // 存储用户信息
    SET_USERINFO: (state, userinfo) => {
      state.userinfo = userinfo
    }
  },
  actions: {
    //获取用户信息
    async getUserinfo({ commit }) {
      const res = await getInfoApi();
      console.log("getUserinfo---store", res);
      if (res.data.code == 0) {
        commit('SET_USERINFO', res.data.data)
        return res.data.data
      }
    },
  },
  modules: {

  },
  plugins: [persistedstate({ paths: ['userinfo'] })]
});
